package com.awspaas.user.apps.cpq.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CPQ_GetWeightNumberWeb {

	public String getWeightNumber(UserContext userContext, String bindId, String clbm, String id, String datas) {
		XSSFWorkbook workbookwl = new XSSFWorkbook();
		try {
			/**
			 * 获取当前项目的基本参数对应值
			 */
        	  JSONArray dataList=JSONObject.parseArray(datas);
        		Map<String, String> pMap = new HashMap<String, String>();
        		Map<String, String> nameMap = new HashMap<String, String>();
        		Map<String, String> noMap = new HashMap<>();
        		for(int i=0;i<dataList.size();i++) {
        			JSONObject jsonObject=(JSONObject) dataList.get(i);
        			String bh=(String) jsonObject.get("bh");
            		String value=(String) jsonObject.get("data");
            		String name=(String) jsonObject.get("name");
            		pMap.put(bh, value);
            		nameMap.put(bh, name);
            		int num = i + 1;
					noMap.put(bh, "B$" + num);

        		}
        		BO data = SDK.getBOAPI().query("BO_EU_CPQ_PROPERTIES").addQuery("CLBM=", clbm).detail();
        		/**
				 * 计算物料数量和成本。
				 */

				/************************** 通过excel公式进行计算 ***********************************/

				// 创建公式评估器
				FormulaEvaluator formulaEvaluator = workbookwl.getCreationHelper().createFormulaEvaluator();
				// 设置单元格值
				Sheet sheet = workbookwl.createSheet("Sheet1");
				for (int i = 0; i < dataList.size(); i++) {
					int num = i + 1;
					Cell cellB1 = sheet.createRow(i).createCell(1);
					JSONObject jsonObject=(JSONObject) dataList.get(i);
        			String bh=(String) jsonObject.get("bh");
					cellB1.setCellValue(pMap.get(bh));
				}
				String equation = data.getString("ZLJSGS");
				// 获取物料数量计算公式中参数代号，如果有则将代号存放在list中
				for (String key : pMap.keySet()) {
					int num = equation.indexOf(key);
					if (num > -1) {
						equation = equation.replace(key, noMap.get(key));
					}
				}
				// 计算出物料数量和当前模块的物料成本价格
				Cell cellB1 = sheet.createRow(0).createCell(2);
				cellB1.setCellFormula(equation);
				formulaEvaluator.evaluateFormulaCell(cellB1);
				// 获取公式计算结果
				CellValue cellValue = formulaEvaluator.evaluate(cellB1);
				System.out.println("The result of the formula is: " + cellValue.getNumberValue());
				double totalWeight = cellValue.getNumberValue();
				String price = data.getString("CLDJ");
				double total = Double.parseDouble(price) * cellValue.getNumberValue();
				//获取当前选择的材料明细表中对应的数据
				BO moduleData = SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART_PROPERTIES_GRID").addQuery("ID=", id).detail();
				moduleData.set("ZL",totalWeight );//模块单价
				moduleData.set("CLF", total);//模块数量
				SDK.getBOAPI().update("BO_EU_CPQ_COMPONENT_PART_PROPERTIES_GRID", moduleData);
				ResponseObject ro = ResponseObject.newOkResponse();
	  			
				return ro.toString();
		} catch (Exception e) {
			throw e;
		}finally {
			try {
				workbookwl.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
